﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using MVCHelpers;

namespace System.Web.Mvc
{
    public static class DropDownListHelpers
    {


        /// <summary>
        /// 
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="name">The name and ID of this DropDownList</param>
        /// <param name="parentID">The ID of the parent DropDownList</param>
        /// <param name="actionName">The Controller Action name. This action must return an JSON object with properties named "ID" and "Text" and receives a parameter named "id", which represents the parent dropdown's selected value. </param>
        /// <param name="controllerName">The Controller name</param>
        /// <param name="initialOption">The only option that is shown at startup (before the action method is triggered)</param>
        /// <param name="optionalOption">An option with no value (text only) that appears before the actual results of the json.</param>
        /// <param name="htmlAttributes"></param>
        /// <returns></returns>
        public static MvcHtmlString CascadingDropDownList(this HtmlHelper helper, string name, string parentID, string actionName, string controllerName, string initialOption, string optionalOption, object htmlAttributes)
        {
            StringBuilder html = new StringBuilder();
            html.Append("<select ");

            PropertyInfo[] htmlProperties = htmlAttributes != null ? htmlAttributes.GetType().GetProperties() : new PropertyInfo[0];

            if (!htmlProperties.Any(p => p.Name.ToLower() == "id"))
                html.AppendFormat("id=\"{0}\" ", name);

            if (!htmlProperties.Any(p => p.Name.ToLower() == "name"))
                html.AppendFormat("name=\"{0}\" ", name);

            foreach (var prop in htmlProperties)
            {
                html.AppendFormat("{0}=\"{1}\" ", prop.Name, prop.GetValue(htmlAttributes, null));
            }

            html.Append(">");

            if (initialOption != null)
                html.AppendFormat("<option value=\"\"> {0} </option>", initialOption);

            html.Append("</select> \n ");
            html.Append("<script type=\"text/javascript\">");

            string url = controllerName == null ?
                new UrlHelper(helper.ViewContext.RequestContext).Action(actionName) :
                new UrlHelper(helper.ViewContext.RequestContext).Action(actionName, controllerName);
            string js = EmbeddedResources.ReadText("CascadingDropDown.js")
                             .Replace("{parentID}", parentID)
                             .Replace("{name}", name)
                             .Replace("{url}", url)
                             .Replace("{optionalOption}", optionalOption)
                             .Replace("  ", " ");

            html.Append(js);

            html.Append("</script>");

            return MvcHtmlString.Create(html.ToString());
        }

        #region CascadingDropDownList Overloads

        public static MvcHtmlString CascadingDropDownList(this HtmlHelper helper, string name, string parentID, string actionName)
        {
            return CascadingDropDownList(helper, name, parentID, actionName, null, null, null, null);
        }

        #endregion

    }
}
